package com.fowo.api.controller.shipment.wfs;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fowo.api.entity.Parilist;
import com.fowo.api.mapper.ParilistMapper;
import com.fowo.api.model.wfs.cargo.WfsCargoAgainmateAction;
import com.fowo.api.model.wfs.cargo.WfsCargoVo;
import com.fowo.api.model.wfs.cargo.item.WfsCargoItemItemVo;
import com.fowo.api.model.wfs.cargo.item.WfsCargoItemVo;
import com.fowo.api.service.WfsCargoItemService;
import com.fowo.api.service.WfsCargoService;
import com.fowo.api.service.impl.WfsCargoItemServiceImpl;
import com.fowo.api.service.impl.WfsCargoServiceImpl;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Component
public class wfsCargoAgainmateAction implements WfsCargoAgainmateAction {

    @Resource
    private ParilistMapper parilistMapper;
    @Resource
    private WfsCargoServiceImpl WfsCargoService;
    @Resource
    protected WfsCargoItemServiceImpl wfsCargoItemService;
    @Override
    public void run(List<Long> idList) throws Exception {
        for(int i=0;i<idList.size();i++){
            handle(idList.get(i));
        }
    }

    private void handle(Long aLong) throws Exception {
        WfsCargoVo voById = WfsCargoService.getVoById(aLong);
        List<WfsCargoItemItemVo> collect = voById.getItems().stream().filter(x -> x.getProductName() == null).collect(Collectors.toList());
        Set<String> mskList = voById.getItems().stream().map(x -> x.getMsku()).collect(Collectors.toSet());
        List<Parilist> parilists = parilistMapper.selectList(Wrappers.lambdaQuery(Parilist.class)
                .eq(Parilist::getShopId, voById.getShopId()).eq(Parilist::getType,"1")
                .in(Parilist::getMsku,mskList));
        collect.forEach(x -> {
            Parilist parilist = parilists.stream().filter(p -> p.getMsku().equals(x.getMsku())).findAny().orElse(null);
            if(parilist!=null){
                WfsCargoItemVo wfsCargoItemVo=new WfsCargoItemVo();
                wfsCargoItemVo.setId(x.getId());
                wfsCargoItemVo.setProductName(parilist.getProductId());
                wfsCargoItemVo.setSku(parilist.getSku());
                try {
                    boolean update = wfsCargoItemService.updateById(wfsCargoItemVo);
                    System.out.println(update);
                }catch (Exception e){
                    System.out.println(e.getMessage());
                }
            }
        });
    }
}
